﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using NetCoreCMS.Common;
using NetCoreCMS.IServices;
using NetCoreCMS.Models;
namespace NetCoreCMS.Web.Areas.Admin.Controllers.Order
{

    public class Order_ConfigController : ManageController
    {
        private const string WEB_VIEW = "~/Areas/Admin/Views/Order/order_config.cshtml";
        public override void OnActionExecuting(ActionExecutingContext context)
        {
            base.OnActionExecuting(context);
            if (context.Result != null)
                return;
            ChkAdminLevel("order_config", CMSEnums.ActionEnum.View.ToString()); //检查权限
        }
        // GET: /admin/Order_Config/

        public ActionResult Index()
        {
            ShowInfo();
            return View(WEB_VIEW);
        }

        [HttpPost]
        public ActionResult SubmitSave()
        {
            ActionResult result = View(EDIT_RESULT_VIEW);
            ChkAdminLevel("order_config", CMSEnums.ActionEnum.Edit.ToString()); //检查权限
            Orderconfig model = Common.XMLConfig.Orderconfig.loadConfig(_env.ContentRootPath);
            try
            {
                model.anonymous = Request.Form["anonymous"].FirstOrDefault().ToLower().IndexOf("true") >= 0 ? 1 : 0;
                model.taxtype = Utils.StrToInt(Request.Form["taxtype"], 1);
                model.taxamount = Utils.StrToDecimal(Request.Form["taxamount"].FirstOrDefault().Trim(), 0);
                model.confirmmsg = Utils.StrToInt(Request.Form["confirmmsg"], 0);
                model.confirmcallindex = Request.Form["confirmcallindex"];
                model.expressmsg = Utils.StrToInt(Request.Form["expressmsg"], 0);
                model.expresscallindex = Request.Form["expresscallindex"];
                model.completemsg = Utils.StrToInt(Request.Form["completemsg"], 0);
                model.completecallindex = Request.Form["completecallindex"];

                Common.XMLConfig.Orderconfig.saveConifg(model,_env.ContentRootPath);
                AddAdminLog(CMSEnums.ActionEnum.Edit.ToString(), "修改订单配置信息"); //记录日志
                ViewBag.ClientScript = JscriptMsg("修改订单配置成功！", "../order_config/index");
            }
            catch
            {
                ViewBag.ClientScript = JscriptMsg("文件写入失败，请检查是否有权限！", string.Empty);
            }
            return result;
        }

        #region 赋值操作=================================
        private void ShowInfo()
        {
            Orderconfig model = Common.XMLConfig.Orderconfig.loadConfig(_env.ContentRootPath);
            ViewData["model"] = model;
        }
        #endregion
    }
}
